<?php

namespace App\Repositories\Criteria;

use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;

/**
 * Class DataRangeFilterCriteria
 * @package namespace App\Repositories\Criteria;
 */
class DateRangeFilterCriteria implements CriteriaInterface
{
    protected $date_start;
    protected $date_end;
    protected $field;


    public function __construct($start_at, $end_at, $field = 'created_at')
    {
        $this->date_start = $start_at;
        $this->date_end = $end_at;
        $this->field = $field;
    }

    /**
     * Apply criteria in query repository
     *
     * @param                     $model
     * @param RepositoryInterface $repository
     *
     * @return mixed
     */
    public function apply($model, RepositoryInterface $repository)
    {
        if (strlen($this->date_start) != 0) {
            $model = $model->where($this->field, '>=', $this->date_start);
        }
        if (strlen($this->date_end) !=0) {
            $model = $model->where($this->field, '<', $this->date_end);
        }
        return $model;
    }
}
